//
//  ChooseViewCell.m
//  KuWoCan
//
//  Created by hwd on 17/7/25.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "ChooseViewCell.h"
#import "CanTextView.h"

@implementation ChooseViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
    }
    return self;
}

- (void)setUpUIWithArr:(NSArray *)btnTitleArr TextStr:(NSString *)textStr ChooseNumber:(NSInteger)chooseNumber;
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    UILabel *leftLab = [UILabel new];
    leftLab.font = KwFont(15);
    leftLab.textColor = KwBlueColor;
    leftLab.text = @"费用核销要求标准";
    [self addSubview:leftLab];
    [leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(@10);
        make.height.equalTo(@30);
    }];
    
    float x= ([UIScreen mainScreen].bounds.size.width - 25*screenW_6*2 - 100*screenW_6*3)/2;
    
    for (int i = 0; i < btnTitleArr.count; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100+i;
        btn.titleLabel.font = KwFont(13*screenW_6);
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitle:btnTitleArr[i][@"title"]?btnTitleArr[i][@"title"]:@"" forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"yuan"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"yuan_sel"] forState:UIControlStateSelected];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10*screenW_6, 0, 0)];
        [btn addTarget:self action:@selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        if (i == chooseNumber) {
            btn.selected = YES;
        }
        
    }
    
    UIButton *currtenBtn;
    UIButton *lastBtn;
    for (int i = 0; i < btnTitleArr.count; i++)
    {
        
        currtenBtn = (UIButton *)[self viewWithTag:100+i];
        lastBtn = [self viewWithTag:100+i-1];
        
        [currtenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (i % 3 == 0)
            {
                make.left.equalTo(@(25*screenW_6));
            }
            else if (i % 3 == 1)
            {
                 make.left.equalTo(lastBtn.mas_right).offset(x);
            }
            else if (i % 3 == 2)
            {
                make.right.equalTo(@(-25*screenW_6));
                make.left.equalTo(lastBtn.mas_right).offset(x);
            }
            make.top.equalTo(leftLab.mas_bottom).offset(30*screenW_6*(i/3));
            make.width.equalTo(@(100*screenW_6));
            make.height.equalTo(@(30*screenW_6));
            
            if (i ==btnTitleArr.count-1 ) {
                make.bottom.equalTo(self.mas_bottom).offset(-10);
            }
        }];
    }
    
//    _textView = [[CanTextView alloc]initWithFrame:CGRectMake(0, 0, SCREENW-30*screenW_6, 0)];
//    [_textView configTextView];
//    _textView.backgroundColor = [UIColor redColor];
//   
//    [self addSubview:_textView];
//    _textView.text = textStr;
//    float  Textheight = [self heightForTextView:_textView WithText:_textView.text];
//    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(@(-25*screenW_6));
//        make.left.equalTo(@(25*screenW_6));
//        make.top.equalTo(lastBtn.mas_bottom).offset(10*screenW_6);
//        make.height.equalTo(@(Textheight));
//        make.bottom.equalTo(self.mas_bottom).offset(-20*screenW_6);
//    }];
//    _textHeight = Textheight;
//     [_textView becomeFirstResponder];
//    __weak typeof(self)weakSelf = self ;
//    _textView.heightblock = ^(float height){
//        NSLog(@"====%f====%f",height,_textHeight);
//        if (height > _textHeight + 20 || height < _textHeight - 20) {
//            if (weakSelf.reloadTextBlock) {
//                weakSelf.reloadTextBlock(weakSelf.textView);
//            }
//            _textHeight = height;
//        }
//        [weakSelf.textView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(@(-25*screenW_6));
//            make.left.equalTo(@(25*screenW_6));
//            make.top.equalTo(lastBtn.mas_bottom).offset(10*screenW_6);
//            make.height.equalTo(@(height));
//            make.bottom.equalTo(weakSelf.mas_bottom).offset(-20*screenW_6);
//        }];
//    };
}


- (void)selectBtn:(UIButton *)btn
{
//    for (UIButton *btn in self.subviews) {
//        btn.selected = NO;
//    }
//    
//    btn.selected = !btn.selected;
    if (_reloadBtnBlock)
    {
        _reloadBtnBlock(btn.tag);
    }
}

@end
